package com.tjsinfo.tjsoa.workplan.bo;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.tjsinfo.tjsoa.common.DateTimeFromConver;
import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.common.TestRiQi;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.workplan.dao.IWorkPlanMainDao;
import com.tjsinfo.tjsoa.workplan.vo.WorkPlanMain;


public class WorkPlanMainBo implements IWorkPlanMainBo{
	final boolean  STARTFALSE=false;
	final boolean  STARTTRUE=true;
	private IWorkPlanMainDao workPlanMainDao;

	public IWorkPlanMainDao getWorkPlanMainDao() {
		return workPlanMainDao;
	}

	public void setWorkPlanMainDao(IWorkPlanMainDao workPlanMainDao) {
		this.workPlanMainDao = workPlanMainDao;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.workplan.bo.IWorkPlanMainBo#delete(java.lang.Integer)
	 */
	public void delete(String id) {
		// TODO Auto-generated method stub
		workPlanMainDao.delete(selectByID(id));
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.workplan.bo.IWorkPlanMainBo#save(java.lang.Integer, java.util.Date, java.lang.String, java.lang.String, java.lang.String)
	 */
	public String save(Integer userid, Date datetime, String type,
			String content, String complete_countent,Integer year,Integer number,Integer start,Integer submittype) {
		// TODO Auto-generated method stub
		WorkPlanMain workPlanMain = new WorkPlanMain();
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		workPlanMain.setTjsUser(tjsUser);
		workPlanMain.setCompleteContent(complete_countent);
		workPlanMain.setContent(content);
		workPlanMain.setTime(datetime);
		workPlanMain.setType(type);
		workPlanMain.setYear(year);
		workPlanMain.setNumber(number);
		workPlanMain.setStart(start);
		workPlanMain.setSubmittype(submittype);
		String id = workPlanMainDao.save(workPlanMain);
		return id;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.workplan.bo.IWorkPlanMainBo#update(java.lang.Integer, java.lang.Integer, java.util.Date, java.lang.String, java.lang.String, java.lang.String)
	 */
	public void update(String id, Integer userid, Date datetime, String type,
			String content, String complete_countent,Integer year,Integer number,Integer start) {
		// TODO Auto-generated method stub
		WorkPlanMain workPlanMain = new WorkPlanMain();
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		workPlanMain=workPlanMainDao.selectOneById(id);
		workPlanMain.setTjsUser(tjsUser);
		workPlanMain.setCompleteContent(complete_countent);
		workPlanMain.setContent(content);
		workPlanMain.setTime(datetime);
		workPlanMain.setType(type);
		//workPlanMain.setId(id);
		workPlanMain.setYear(year);
		workPlanMain.setStart(start);
		workPlanMain.setNumber(number);
		workPlanMainDao.update(workPlanMain);
	}
	/**
	 * 根据用户ID时间类型查询工作计划
	 * @param 用户
	 * @param 时间
	 * @param 类型
	 * @return
	 */
	public WorkPlanMain selectTypeAndUserAndDate(Integer userid,Date date,String type)
	{
			return  workPlanMainDao.selectDateTime(date, type, userid);
	}
	
	/**
	 * 基于主键查询
	 * @param id
	 * @return
	 */
	public WorkPlanMain selectByID(String id)
	{
		return workPlanMainDao.selectOneById(id);
	}
	/**
	 * 查询工作计划并且带批示

	 * @param jihuaid
	 * @return
	 */
	public WorkPlanMain selectWorkPlanMainAndExamine(String jihuaid)
	{
		return  workPlanMainDao.selectWorkPlan(jihuaid);
	}
	/**
	 * 添加工作计划完成情况
	 * @param value
	 * @param id
	 * @return
	 */
	public boolean addWorkPlanMainCompleteContent(String value,String id)
	{
		try {
			WorkPlanMain workPlanMain = workPlanMainDao.selectWorkPlan(id);
		//	WorkPlanMain workPlanMain = new WorkPlanMain();
			workPlanMain.setCompleteContent(value);
			//workPlanMain.setId(id);
			//iWorkPlanMainDao.addWorkPlanMainCompleteContent(workPlanMain);
			workPlanMain.setStart(3);
			 workPlanMainDao.update(workPlanMain);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}
	/**
	 * 根据年份和类型查询

	 * @param 类型
	 * @param 年

	 * @return
	 */
	public List selectWorkPlanTypeAndYear(String type,Integer year,Integer userid)
	{
		try {
			WorkPlanMain workPlanMain = new WorkPlanMain();
			workPlanMain.setType(type);
			workPlanMain.setYear(year);
			TSystemUser tjsUser = new TSystemUser();
			tjsUser.setUserId(userid);
			workPlanMain.setTjsUser(tjsUser);
			return  workPlanMainDao.selectWorkPlanMainYearAndTypeAndUser(workPlanMain);
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 根据年份和类型查询

	 * @param 类型
	 * @param 年

	 * @return map
	 */
	public Map<Date, WorkPlanMain> selectWorkPlanTypeAndYearMAP(String type,Integer year,Integer userid)
	{
		Map<Date, WorkPlanMain> map = new HashMap<Date, WorkPlanMain>();
		List list =  selectWorkPlanTypeAndYear(type, year,userid);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			WorkPlanMain workPlanMain = (WorkPlanMain) iterator.next();
			map.put(workPlanMain.getTime(), workPlanMain);
		}
		return map;
	}
	/**
	 * 根据年份和类型查询

	 * @param 类型
	 * @param 年

	 * @return map<"number","workplan">
	 */
	public Map<Integer, WorkPlanMain> selectWorkPlanTypeAndYearMAPNumber(String type,Integer year,Integer userid)
	{
		Map<Integer, WorkPlanMain> map = new HashMap<Integer, WorkPlanMain>();
		List list =  selectWorkPlanTypeAndYear(type, year,userid);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			WorkPlanMain workPlanMain = (WorkPlanMain) iterator.next();
			map.put(workPlanMain.getNumber(), workPlanMain);
		}
		return map;
	}
	
	/**
	 * 查询状态类型和年份
	 */
	public Map<Date, WorkPlanMain> selectWorkPlanTypeAndYearMAPTimeStart(
			String type, Integer year, Integer userid, Integer start) {
		// TODO Auto-generated method stub
		WorkPlanMain workPlanMain = new WorkPlanMain();
		workPlanMain.setType(type);
		workPlanMain.setYear(year);
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		workPlanMain.setTjsUser(tjsUser);
		workPlanMain.setStart(start);
		Map<Date, WorkPlanMain> map = new HashMap<Date, WorkPlanMain>();
		List list = workPlanMainDao.selectWorkPlanMainYearAndTypeAndUser(workPlanMain);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			WorkPlanMain PlanMain = (WorkPlanMain) iterator.next();
			map.put(PlanMain.getTime(), PlanMain);
		}
		
		return map;
	}
	/**
	 * 检查默认工作日志日日志
	 */
	public Map<Date, WorkPlanMain> selectDefantWorkPlanDate(
			String type, Integer year, Integer userid, Integer start) {
		// TODO Auto-generated method stub
		
		WorkPlanMain workPlanMain = new WorkPlanMain();
		workPlanMain.setType(type);
		workPlanMain.setYear(year);
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		workPlanMain.setTjsUser(tjsUser);
		workPlanMain.setStart(start);
		Map<Date, WorkPlanMain> map = new HashMap<Date, WorkPlanMain>();
		workPlanMain.setSubmittype(0);
		//List list = selectWorkPlanTypeAndYearListNumberStart(workPlanMain);
		List list = workPlanMainDao.selectDefanultDateWorkPlan(workPlanMain);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			WorkPlanMain PlanMain = (WorkPlanMain) iterator.next();
			map.put(PlanMain.getTime(), PlanMain);
		}		
		return map;
	}
	
	/**
	 * 检查默认工作计划

	 */
	public Map<Date, WorkPlanMain> checkDefinntWorkPlan(
			String type, Integer year, Integer userid, Integer start) {
		// TODO Auto-generated method stub
		WorkPlanMain workPlanMain = new WorkPlanMain();
		workPlanMain.setType(type);
		workPlanMain.setYear(year);
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		workPlanMain.setTjsUser(tjsUser);
		workPlanMain.setStart(start);
		workPlanMain.setSubmittype(0);
		Map<Date, WorkPlanMain> map = new HashMap<Date, WorkPlanMain>();
		List list = selectWorkPlanTypeAndYearListNumberStart(workPlanMain);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			WorkPlanMain planMain = (WorkPlanMain) iterator.next();
			map.put(planMain.getTime(), planMain);
		}
		
		return map;
	}
	/**
	 * 查询状态类型和年份
	 */
	public Map<Integer, WorkPlanMain> selectWorkPlanTypeAndYearMAPNumberStart(
			String type, Integer year, Integer userid, Integer start) {
		// TODO Auto-generated method stub
		WorkPlanMain workPlanMain = new WorkPlanMain();
		workPlanMain.setType(type);
		workPlanMain.setYear(year);
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		workPlanMain.setTjsUser(tjsUser);
		workPlanMain.setStart(start);
		workPlanMain.setSubmittype(0);
		Map<Integer, WorkPlanMain> map = new HashMap<Integer, WorkPlanMain>();
		List list = workPlanMainDao.selectDefanultDateWorkPlan(workPlanMain);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			WorkPlanMain PlanMain = (WorkPlanMain) iterator.next();
			map.put(PlanMain.getNumber(), PlanMain);
		}
		
		return map;
	}
	private  List selectWorkPlanTypeAndYearListNumberStart(WorkPlanMain workPlanMain)
	{
		List list =  workPlanMainDao.selectWorkPlanMainYearAndTypeAndStart(workPlanMain);
		return list;
	}
	/**
	 * 根据用户和类型

	 */
	public List selectTypeAndUser(String type, Integer id) {
		// TODO Auto-generated method stub
		WorkPlanMain planMain = new WorkPlanMain();
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(id);
		planMain.setType(type);
		planMain.setTjsUser(tjsUser);
		return workPlanMainDao.selectWorkPlanMainUserAndType(planMain);
		
	}
	/**
	 * 查询用户和时间

	 */
	public WorkPlanMain  selectDateAndUser(String datestr,Integer userid)
	{
		WorkPlanMain workPlanMain = new WorkPlanMain();
		
		workPlanMain.setTime(DateTimeFromConver.datetostr(datestr));
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		workPlanMain.setTjsUser(tjsUser);
		return 	workPlanMainDao.selectWorkPlanDateDayUser(workPlanMain);
	}
	
	/**
	 * 工作计划模糊查询
	 * @param 用户ID
	 * @param 内容关键字

	 * @return
	 */
	public List selectLike(Integer userid, String content) {
		// TODO Auto-generated method stub
		WorkPlanMain workPlanMain = new WorkPlanMain();
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		workPlanMain.setContent(content);
		workPlanMain.setTjsUser(tjsUser);
		return 	workPlanMainDao.selectLikeWorkPlan(workPlanMain);
		
	}
	/**
	 * 工作计划类型模糊查询
	 * @param 用户ID
	 * @param 内容
	 * @param 类型
	 * @return
	 */
	public List selectLikeType(Integer userid, String content,
			String type) {
		// TODO Auto-generated method stub
		WorkPlanMain workPlanMain = new WorkPlanMain();
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		workPlanMain.setContent(content);
		workPlanMain.setTjsUser(tjsUser);
		workPlanMain.setType(type);
		workPlanMain.setContent(content);
		return workPlanMainDao.selectLikeWorkPlanType(workPlanMain);
	}
	
	/**
	 * 返回右侧信息
	 * @param request
	 * @return
	 */
	public void selectMapWorkPlanMain(HttpServletRequest request)
	{
		Date date = new Date();
		Integer id =SessionUser.getUserId(request);
		WorkPlanMain planMain =	selectDateAndUser(DateTimeFromConver.zhuanhuariqi(date), id);
		WorkPlanMain planMaintomorrow =	selectDateAndUser(DateTimeFromConver.zhuanhuariqi(TestRiQi.afterNDay(date, 1)), SessionUser.getUserId(request));
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("今日工作", planMain);
		
		map.put("明日工作", planMaintomorrow);
		request.setAttribute("rightmap", map);
		
	}
}
